package com.samehope.ar.service.impl;

import com.samehope.ar.constant.DelFlagEnum;
import com.samehope.ar.entity.Device;
import com.samehope.ar.mapper.DeviceMapper;
import com.samehope.ar.service.IDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.samehope.ar.util.SessionUtil;
import com.samehope.ar.vo.DeviceVO;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 设备 服务实现类
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@Service
public class DeviceServiceImpl extends ServiceImpl<DeviceMapper, Device> implements IDeviceService {

    /**
     * 获取当前企业绑定的设备列表
     * @return
     */
    @Override
    public List<DeviceVO> devices() {
        return this.baseMapper.findDevicesByCompany(SessionUtil.getCompanyId());
    }

    /**
     * 解绑设备
     * @param deviceId
     */
    @Override
    public void unbind(Long deviceId) {
        Device device = this.baseMapper.findDeviceByCompanyAndDevice(SessionUtil.getCompanyId(), deviceId);

        if (device == null) {
            throw new IllegalArgumentException("设备不存在");
        }

        device.setUpdateBy(SessionUtil.getUserId());
        device.setUpdateDate(LocalDateTime.now());
        device.setDelFlag(DelFlagEnum.DELETED.getCode());
        this.updateById(device);
    }
}
